<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
               :disabled="method==='view'"
               label-width="120px">
        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item label="头像" prop="image"
                          :rules="[
                 ]">
              <el-image
                style="height: 50px;width:50px;margin-right:10px;"
                :src="inputForm.image" @click="showImg(inputForm.image)">
              </el-image>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.phone" placeholder="请填写手机号(账号唯一)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="password"
                          :rules="[
                 ]">
              <el-input type="password" v-model="inputForm.password" placeholder="请填写密码"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="昵称" prop="nikeName"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.nikeName" placeholder="请填写昵称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否为管理员" prop="whetherAdmin" :rules="[]">
              <el-radio v-model="inputForm.whetherAdmin" label="0">否</el-radio>
              <el-radio v-model="inputForm.whetherAdmin" label="1">是</el-radio>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
              <el-form-item label="微信登录" prop="wxOpenid"
                  :rules="[
                   ]">
                <el-input v-model="inputForm.wxOpenid" placeholder="请填写微信登录"     ></el-input>
             </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="支付宝授权登录" prop="zfbOpenid"
                  :rules="[
                   ]">
                <el-input v-model="inputForm.zfbOpenid" placeholder="请填写支付宝授权登录"     ></el-input>
             </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="平度之窗授权登录" prop="pdOpenid"
                  :rules="[
                   ]">
                <el-input v-model="inputForm.pdOpenid" placeholder="请填写平度之窗授权登录"     ></el-input>
             </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="备注信息" prop="remarks"
                          :rules="[
                 ]">
              <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      inputForm: {
        id: '',
        phone: '',
        password: '',
        image: '',
        nikeName: '',
        wxOpenid: '',
        zfbOpenid: '',
        pdOpenid: '',
        remarks: '',
        whetherAdmin:''
      }
    }
  },
  components: {},
  methods: {
    showImg(url) {
      this.$hevueImgPreview(url)
    },
    init(method, id) {
      this.method = method
      this.inputForm.id = id
      if (method === 'add') {
        this.title = `新建用户`
      } else if (method === 'edit') {
        this.title = '修改用户'
      } else if (method === 'view') {
        this.title = '查看用户'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          this.$http({
            url: `/userinfo/tUserInfo/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data.tUserInfo)
            this.loading = false
          })
        }
      })
    },
    // 表单提交
    doSubmit() {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/userinfo/tUserInfo/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            this.loading = false
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
          })
        }
      })
    }
  }
}
</script>
